package action.back;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.Zone;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

public class ZoneAction extends DefaultAction {
	
	private Zone zone;
	
	public String index() {
		return "index";
	}
	
	@SuppressWarnings("unchecked")
	public void getTrees() {
		Integer parent_id = Integer.parseInt(request.getParameter("parent_id"));

		List<Zone> zones;
		if (parent_id == null || parent_id.intValue() == 0) {
			zones = getCommonService().findAll(Zone.class, "position asc", "parent is null",null);
		} else {
			zones = getCommonService().findAll(Zone.class, "position asc", "parent.id = ? ",new Object[]{parent_id});
		}

		List<Map> trees = new ArrayList<Map>();
		for (Zone zone : zones) {
			Map<String, Object> tree = new HashMap<String, Object>();
			tree.put("id", zone.getId());
			tree.put("text", zone.getName());
			tree.put("iconCls", "admin-tool-zone");
			tree.put("leaf", zone.getSubZoneCount() == 0);
			trees.add(tree);
		}
		
		JSONArray jsonObject = JSONArray.fromObject(trees);
		String jsonString = jsonObject.toString();
		renderJson(jsonString);
		
	}
	
	@SuppressWarnings("unchecked")
	public void getChildren() {
		Integer parent_id = Integer.parseInt(request.getParameter("parent_id"));
	        
	    List<Zone> zones;
	    if (parent_id == null || parent_id.intValue() == 0) {
			zones = getCommonService().findAll(Zone.class, "position asc", "parent is null",null);
		} else {
			zones = getCommonService().findAll(Zone.class, "position asc", "parent.id = ? ",new Object[]{parent_id});
		}
	    List<Map<String, Object>> jsonList = new ArrayList<Map<String,Object>>();
	    for(Zone m : zones){
	    	Map<String, Object> map = new HashMap<String, Object>();
	    	map.put("zone", m);
	    	
	    	jsonList.add(map);
	    }
	    JsonConfig config = new JsonConfig();
	    //config.setExcludes(new String[]{"parent"});
	    JSONArray jsonObject = JSONArray.fromObject(jsonList,config);
		String jsonString = jsonObject.toString();
		renderJson(jsonString);
	}
	


	public void update() {
		if(zone!=null&&zone.getCode()!=null)zone.setCode(zone.getCode().trim());
		getCommonService().update(zone);
		renderJson("{success:true,id:"+zone.getId()+",name:'"+zone.getName()+"'}");
	}
	
	public void create() {
		
		Integer parent_id = Integer.parseInt(request.getParameter("parent_id"));
		if(parent_id>0){
			Zone parent = new Zone();
			parent.setId(parent_id);
			zone.setParent(parent);
		}
		
		//zone.setIfFunc("false");
		getCommonService().save(zone);

		renderJson("{success:true,id:"+zone.getId()+"}");
	}
	
	

	public void delete() {
		for(Integer id : ids){
			getCommonService().executeUpdate("update Orgn e set e.zone = null where e.zone.id = ?", id);
			getCommonService().executeUpdate("delete Zone where id = ?", id);
		}
		
		renderJson("{success:true}");
	}
	

	public Zone getZone() {
		return zone;
	}

	public void setZone(Zone zone) {
		this.zone = zone;
	}
}
